package com.shengde.jinwawaforleader.base;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

import com.shengde.jinwawaforleader.utils.ActivityHelper;
import com.shengde.jinwawaforleader.utils.ActivityStackUtil;

/**
 * Created by Jyq on 2016/1/10.
 */
public abstract class BaseActivity extends Activity implements
		View.OnClickListener {
	// 回调跳转界面
	private IntentCallBack intentCallBack;
	private Toast toast;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		onRoot(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		ActivityStackUtil.add(this);
		intentCallBack = new IntentCallBack() {
			@Override
			public Intent startActivityCommon(Class cls) {
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), cls);
				startActivity(intent);
				return intent;
			}

			@Override
			public Intent startActivityForResultCommon(Class cls, int result) {
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), cls);
				startActivityForResult(intent, result);
				return intent;
			}

			@Override
			public Intent startActivityFinishCommon(Class cls) {
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), cls);
				startActivity(intent);
				finish();
				return intent;
			}
		};
		initView();
		initData();
		initListener();
	}
	@Override
	public void onClick(View v) {
		onWidgetClick(v);
	}

	public IntentCallBack getIntentCallBack() {
		return intentCallBack;
	}

	public interface IntentCallBack {
		Intent startActivityCommon(Class cls);

		Intent startActivityForResultCommon(Class cls, int result);

		Intent startActivityFinishCommon(Class cls);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		return ActivityHelper.createLoadingDialog(this);
	}

	@Override
	protected Dialog onCreateDialog(int id, Bundle args) {
		return ActivityHelper.createLoadingDialog(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		ActivityStackUtil.remove(this);
	}

	// 设置内容
	public abstract void onRoot(Bundle savedInstanceState);

	// 初始化控件
	public abstract void initView();

	// 初始化数据
	public abstract void initData();

	// 初始化控件点击事件
	public abstract void initListener();

	// 初始化控件点击事件
	public abstract void onWidgetClick(View view);

	/**
	 * toast提示
	 * @param msg
     */
	public void showToast(String msg){
		if(toast!=null) {
		    toast.cancel();
		}
		toast = Toast.makeText(this,msg,Toast.LENGTH_SHORT);
		toast.show();
	}
}